//
//  FYComposePictureView.swift
//  Mircore
//
//  Created by apple on 16/8/16.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit


//重用标记

private  let  FYComposePictureViewCellIdentifier  = "FYComposePictureViewCellIdentifier"


//发微博配图

class FYComposePictureView: UICollectionView {
    
    //点击加号按钮执行的闭包
    
    var didSeletedAddImageViewClosure:(() -> ())?
    
    //图片数据源
    
    lazy  var  images: [UIImage]  = [UIImage]()
    
    
    override  init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout  = UICollectionViewFlowLayout()
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        
        fatalError("init(coder:) has  not been implemented")
    }
    
    //设置控件
    
    private  func  setupUI() {
        
        //默认状态当前配图隐藏
        
        hidden = true
        
        
        //注册cell
        
        registerClass(FYComposePictureViewCell.self, forCellWithReuseIdentifier: FYComposePictureViewCellIdentifier)
        
        //设置数据源代理
        dataSource  =  self
        
        //设置代理
        
        delegate = self
    }
    
    
    //添加图片方法
    
    func  addImage(image: UIImage)  {
        
        //最多显示9张图片
        
        if  images.count > 9 {
            
            return
        }
        
        //显示配图
        
        hidden = false
        //添加图片
        images.append(image)
        
        //重写加载数据
        
        self.reloadData()
        
    }
    
    
    override  func  layoutSubviews() {
        
        super.layoutSubviews()
        
        
        //设置每项的间距
        
        let  itemMargin: CGFloat  = 5
        
        //计算每项的大小
        
        let  itemWidth  = (width  - 2 * itemMargin)  / 3
        
        
        //获取布局方式
        
        let  flowLayout  =  self.collectionViewLayout as!   UICollectionViewFlowLayout
        
        //设置大小
        
        flowLayout.itemSize  = CGSize(width: itemWidth, height: itemWidth)
        
        //设置水平间距
        
        flowLayout.minimumInteritemSpacing  = itemMargin
        
        //设置垂直间距
        
        flowLayout.minimumLineSpacing  = itemMargin
        
    }
}


//MARK:-UICollectionViewDataSource实现数据源代理方法

extension   FYComposePictureView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func  collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        //如果是0张或者9张图片  那么不需要多显示一个单元格
        
        if  images.count == 0  || images.count == 9  {
            
            return  images.count
            
        }else  {
            
            //否则要多显示一个加号的cell
            
            return images.count + 1
            
        }
        
    }
    
    
    func  collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let  cell = collectionView.dequeueReusableCellWithReuseIdentifier(FYComposePictureViewCellIdentifier, forIndexPath: indexPath) as!  FYComposePictureViewCell
        
        //indexPath.item索引等于数组的个数表示最后一个cell
        
        if indexPath.item == images.count  {
            
            cell.image = nil
            
        }else {
            
            //绑定图片
            
            cell.image  = images[indexPath.item]
            
            //设置删除图片闭包
            
            cell.deleteButtomClosure  = {  [weak self]  in
                
                //通过数据源删除指定图片
                
                self?.images.removeAtIndex(indexPath.item)
                
                //判断数据源是否为0
                
                if self?.images.count == 0 {
                    
                    //隐藏当前配图
                    
                    self?.hidden  = true
                }
                
                self?.reloadData()
            }
            
            
        }
        
        
        return cell
    }
    
    
    func  collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        //如果索引等于我们数组的个数表示最后一个cell
        
        if  indexPath.item  == images.count  {
            
            //取消选中
            
            collectionView.deselectItemAtIndexPath(indexPath, animated: true)
            
            
            //执行闭包
            
            didSeletedAddImageViewClosure?()
            
        }
    }
    
}
//自定义发微博图片cell

class    FYComposePictureViewCell: UICollectionViewCell  {
    
    //点击删除按钮执行的闭包
    
    var  deleteButtomClosure: (() -> ())?
    
    var  image: UIImage? {
        
        didSet  {
            
            if   image == nil {
                
                //添加加号按钮图片
                imageView.image = UIImage(named: "compose_pic_add")
                
                //添加高亮图片
                
                imageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
                //如果是设置加号图片那么隐藏删除按钮
                
                deleteButton.hidden = true
            }else {
                
                //设置绑定的图片
                
                imageView.image = image
                
                //显示删除按钮
                
                deleteButton.hidden = false
                
                //删除高亮图片
                
                imageView.highlightedImage = nil
                
            }
            
            
        }
        
    }
    
    
    
    //MARK:-懒加载控件
    
    //显示图片
    
    private  lazy  var  imageView: UIImageView = UIImageView()
    
    //删除按钮
    
    private  lazy  var deleteButton: UIButton  = {
        
        let  button = UIButton()
        
        
        //添加点击事件
        
        button.addTarget(self, action: #selector(FYComposePictureViewCell.deleteButtonAction), forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: "compose_photo_close"), forState: .Normal)
        
        return button
        
    }()
    
    override  init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder: ) has not been implemented")
    }
    
    //添加控件设置约束
    
    private  func  setupUI() {
        
        contentView.addSubview(imageView)
        
        contentView.addSubview(deleteButton)
        
        imageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        deleteButton.snp_makeConstraints { (make) in
            
            make.top.equalTo(imageView)
            
            make.trailing.equalTo(imageView)
        }
    }
    
    //MARK:-监听点击事件
    
    @objc  private func  deleteButtonAction() {
        
        //执行删除图片的闭包
        
        deleteButtomClosure?()
    }
}

